<?php

namespace Models;

use Models\Common\MoneyLogRecord;
use Models\Common\MoneyLogTrait;

class Recharge extends Model implements MoneyLogRecord
{
    use MoneyLogTrait;

    protected $_table = 'recharges';

    protected $_filter = FALSE;

    protected $_primekey = 'id';

    protected static $presents = null;


    /**
     * 获取赠送金额
     * @param int|string $money 赠送金额
     */
    public static function getPresent($money)
    {
        if (null === self::$presents) {
            self::$presents = (new Config)->where('label', '充值设置')
                ->order_by('value1')->get();
        }

        $add = 0;

        foreach (self::$presents as $key => $value) {
	    	if ($value['value'] <= $money) {
	    		$add = $value['value1'];
	    	}
	    }

        return $add;
    }

    /* {@inheritDoc} */
    public function getMoneyType()
    {
        return 2;
    }

    public function getMoneyLogData($data = null)
    {
        return [
            'type' => isset($data['type']) ? $data['type'] : $this->getMoneyType(),
            'user_id' => $this['user_id'],
            'cashier_id' => isset($data['cashier_id']) ? $data['cashier_id'] : '',
            'relation_id' => $this['id'],
            'num' => $this['recharge'],
            'last' => isset($data['last']) ? $data['last'] : '',
            'now' => isset($data['now']) ? $data['now'] : '',
            'desc' => isset($data['desc']) ? $data['desc'] : '',
            'pay_type' => isset($data['pay_type']) ? $data['pay_type'] : '',
            'from' => isset($data['from']) ? $data['from'] : '',
            'create_time' => date('Y-m-d H:i:s')
        ];
    }
}
